#include <atomic>
#include <iostream>
#include <thread>

std::atomic<int> x(0);

void thread1() {
    int expected = 0;
    // 尝试将x从0改为1，同时确保这个操作的释放和获取语义
    if (x.compare_exchange_strong(expected, 1, std::memory_order_acq_rel)) {
        std::cout << "Thread 1: Successfully changed." << std::endl;
    }
}

void thread2() {
    int expected = 1;
    // 尝试将x从1改为2，同样确保释放和获取语义
    if (x.compare_exchange_strong(expected, 2, std::memory_order_acq_rel)) {
        std::cout << "Thread 2: Successfully changed." << std::endl;
    }
}

int main() {
    std::thread t1(thread1);
    std::thread t2(thread2);
    t1.join();
    t2.join();
    std::cout << "Final value of x: " << x.load() << std::endl;
}